package com.woorigil.social.domain;

import com.ibatis.sqlmap.client.SqlMapClient;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import java.io.Serializable;

/**
 * Created with IntelliJ IDEA.
 * User: Nicejinux
 * Date: 13. 7. 30.
 * Time: 오전 12:21
 * To change this template use File | Settings | File Templates.
 */
public abstract class FlowerSqlMapDaoSupport<T, I extends Serializable> extends SqlMapClientDaoSupport implements BaseDao<T, I> {
    abstract public String getNamespace();

    @Autowired
    public void setFlowerSqlMapClient(SqlMapClient flowerSqlMapClient) {
        this.setSqlMapClient(flowerSqlMapClient);
    }

    public String getStatementName(String namespace, String id) {
        return StringUtils.trimToEmpty(namespace) + "." + StringUtils.trimToEmpty(id);
    }

    @SuppressWarnings("unchecked")
    @Override
    public I insert(T entity) {
        return (I)getSqlMapClientTemplate().insert(getNamespace() + ".insert", entity);
    }

    @Override
    public void update(T entity) {
        getSqlMapClientTemplate().update(getNamespace() + ".update", entity);
//        getFlowerSqlMapClientTemplate().update(getNamespace() + ".update", entity);
    }

    @Override
    public void delete(I id) {
        getSqlMapClientTemplate().delete(getNamespace() + ".delete", id);
    }

    @SuppressWarnings("unchecked")
    @Override
    public T select(I id) {
        return (T)getSqlMapClientTemplate().queryForObject(getNamespace() + ".select", id);
    }
}
